#
# Copyright 2023 The titan-search Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

kmcmake_cc_binary(
        NAME benchmark
        SOURCES benchmark.cc
        COPTS ${USER_CXX_FLAGS}
        LINKS
        spica::spica_static
        ${KMCMAKE_DEPS_LINK}
)
kmcmake_cc_binary(
        NAME client
        SOURCES client.cc
        COPTS ${USER_CXX_FLAGS}
        LINKS
        spica::spica_static
        ${KMCMAKE_DEPS_LINK}
)

kmcmake_cc_binary(
        NAME hello
        SOURCES hello.cc
        COPTS ${USER_CXX_FLAGS}
        LINKS
        spica::spica_static
        ${KMCMAKE_DEPS_LINK}
)


kmcmake_cc_binary(
        NAME one_time_request
        SOURCES one_time_request.cc
        COPTS ${USER_CXX_FLAGS}
        LINKS
        spica::spica_static
        ${KMCMAKE_DEPS_LINK}
)

kmcmake_cc_binary(
        NAME redirect
        SOURCES redirect.cc
        COPTS ${USER_CXX_FLAGS}
        LINKS
        spica::spica_static
        ${KMCMAKE_DEPS_LINK}
)

kmcmake_cc_binary(
        NAME server
        SOURCES server.cc
        COPTS ${USER_CXX_FLAGS}
        LINKS
        spica::spica_static
        ${KMCMAKE_DEPS_LINK}
)

kmcmake_cc_binary(
        NAME server_and_client
        SOURCES server_and_client.cc
        COPTS ${USER_CXX_FLAGS}
        LINKS
        spica::spica_static
        ${KMCMAKE_DEPS_LINK}
)
kmcmake_cc_binary(
        NAME simplecli
        SOURCES simplecli.cc
        COPTS ${USER_CXX_FLAGS}
        LINKS
        spica::spica_static
        ${KMCMAKE_DEPS_LINK}
)

kmcmake_cc_binary(
        NAME simplesvr
        SOURCES simplesvr.cc
        COPTS ${USER_CXX_FLAGS}
        LINKS
        spica::spica_static
        ${KMCMAKE_DEPS_LINK}
)

kmcmake_cc_binary(
        NAME ssesvr
        SOURCES ssesvr.cc
        COPTS ${USER_CXX_FLAGS}
        LINKS
        spica::spica_static
        ${KMCMAKE_DEPS_LINK}
)

kmcmake_cc_binary(
        NAME ssecli
        SOURCES ssecli.cc
        COPTS ${USER_CXX_FLAGS}
        LINKS
        spica::spica_static
        ${KMCMAKE_DEPS_LINK}
)

kmcmake_cc_binary(
        NAME upload
        SOURCES upload.cc
        COPTS ${USER_CXX_FLAGS}
        LINKS
        spica::spica_static
        ${KMCMAKE_DEPS_LINK}
)


if(SPICA_OPENSSL_SUPPORT)
    if (OPENSSL_VERSION VERSION_LESS "3.2.0")
        set(OPENSSL_X509_FLAG "-x509")
    else()
        set(OPENSSL_X509_FLAG "-x509v1")
    endif()
    message(STATUS "openssl include path ${OPENSSL_INCLUDE_DIR}")
    find_program(OPENSSL_COMMAND
            NAMES openssl
            HINTS ${OPENSSL_INCLUDE_DIR}/../tools/openssl
            REQUIRED
            )
    if(NOT OPENSSL_COMMAND)
        message(STATUS "not found openssl")
        find_program(OPENSSL_COMMAND
                NAMES openssl
                HINTS ${OPENSSL_INCLUDE_DIR}/../bin
                REQUIRED
                )
    else ()
        message(STATUS "found openssl ${OPENSSL_COMMAND}")
    endif ()
    execute_process(
            COMMAND ${OPENSSL_COMMAND} genrsa 2048
            OUTPUT_FILE key.pem
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
            COMMAND_ERROR_IS_FATAL ANY
    )
    execute_process(
            COMMAND ${OPENSSL_COMMAND} req -new -key key.pem -config ${CMAKE_CURRENT_LIST_DIR}/ex.conf
            COMMAND ${OPENSSL_COMMAND} x509 -days 3650 -req -signkey key.pem
            OUTPUT_FILE cert.pem
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
            COMMAND_ERROR_IS_FATAL ANY
    )
endif()
file(COPY ca-bundle.crt DESTINATION ${CMAKE_CURRENT_BINARY_DIR})